<template>
  <div class="index-page">
    <div class="right-opt">
      <el-button type="default" @click="toAdd">
        <i class="el-icon-circle-plus"></i> 新增</el-button>
    </div>
    <el-form :model="form" class="search-form">
      <el-form-item>
        <el-input v-model="form.customerName" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" type="primary">查询</el-button>
        <el-button @click="reset" type="default">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="e-table">
      <!-- <top-opt class="table-filter" :isBack="false">
        <template slot="left">
          <el-button type="primary" @click="toAdd">
            <i class="el-icon-circle-plus"></i> 新增</el-button>
        </template>
      </top-opt> -->
      
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        border>
        <el-table-column
          label="序号"
          width="60"
          align="center">
          <template slot-scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="customerName"
          label="客户名称">
        </el-table-column>
        <el-table-column
          prop="contact"
          label="联系电话">
        </el-table-column>
        <el-table-column
          prop="address"
          label="工程详细地址">
        </el-table-column>
        <el-table-column
          prop="area"
          label="工程面积">
        </el-table-column>
        <el-table-column
          prop="period"
          label="施工工期">
        </el-table-column>
        <el-table-column
          label="创建时间">
          <template  slot-scope="scope">
            <span>{{formatDate(new Date(scope.row.createDate), 'yyyy-MM-dd hh:mm:ss')}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="toDetail(scope.row)">详情</el-button>
            <el-button type="text" @click="toDel(scope.row)">删除</el-button>
            <el-button type="text" @click="toPrint(scope.row)">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
      <e-page :total="tableTotal" @handleCurrentChange="handleCurrentChange"></e-page>
    </div>
    <project-dialog ref="projectDialog" @handleSuccess="handleSuccess"></project-dialog>
    <print-dialog ref="printDialog"></print-dialog>
  </div>
</template>

<script>
import eDialog from '@/components/dialog';
import ePage from '@/components/pagination';
import projectDialog from './project-dialog';
import printDialog from './print-dialog';
import topOpt from '@/components/top-opt';
import {getProject, delProject} from '@/utils/api';
import storage from '@/utils/storage';
import {formatDate} from '@/utils/format';
export default {
  components: {eDialog, ePage, projectDialog, topOpt, printDialog},
  data () {
    return {
      formatDate,
      form: {
        customerName: '',
        beginDate: '',
        endDate: ''
      },
      userValue: {},
      tableData: [],
      tableTotal: 0,
      loading: false,
      page: {
        pageIndex: 0,
        pageSize: 10
      }
    }
  },
  created () {
    this.userValue = storage.getItem('user_value');
    if (this.userValue) {
      this.getList();
    } else {
      this.$router.push('/login');
    }
  },
  methods: {
    reset () {
      this.form = {
        customerName: '',
        beginDate: '',
        endDate: ''
      };
      this.getList();
    },
    getList () {
      this.loading = true;
      const params = Object.assign({}, this.page, this.form, {adminId: this.userValue.id});
      if (params.time) {
        params.beginDate = formatDate(new Date(params.time[0]), 'yyyy-MM-dd hh:mm:ss');
        params.endDate = formatDate(new Date(params.time[1]), 'yyyy-MM-dd hh:mm:ss');
        delete params.time;
      }
      getProject(params).then((res) => {
        this.loading = false;
        this.tableData = res.content;
        this.tableTotal = res.totalElements;
      }).catch((err) => {
        this.loading = false;
      })
    },
    handleCurrentChange (val) {
      this.page.pageIndex = val - 1;
      this.getList();
    },
    toAdd () {
      this.$refs.projectDialog.openDialog();
    },
    toEdit (item) {
      this.$refs.projectDialog.openDialog(item);
    },
    toDetail (item) {
      this.$router.push('/detail/' + item.id);
    },
    handleSuccess () {
      this.getList();
    },
    toDel (item) {
      this.$confirm('确定要删除该工程吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delProject({id: item.id}).then((res) => {
          this.getList();
        })
      }).catch(() => {      
      });
    },
    toPrint (item) {
      this.$refs.printDialog.openDialog(item.id);
    }
  }
}
</script>

<style lang="scss" scoped>
.index-page {
  position: relative;
  .right-opt {
    position: absolute;
    right: 20px;
    top: -28px;
  }
}
</style>
